<?php

/**
 *  课程表
 */

class Controller_syllabus extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->absent   = YOU::loadModel('absent');
		$this->model->syllabus = YOU::loadModel('syllabus');
		$this->model->grade    = YOU::loadModel('grade');
		$this->model->class    = YOU::loadModel('class');
		$this->model->subject  = YOU::loadModel('subject');
		$this->model->staff    = YOU::loadModel('staff');
		$this->model->semester = YOU::loadModel('semester');
		$this->model->excel    = YOU::loadModel('excel');
		$this->model->special  = YOU::loadModel('special');
	}

	/** 代课课程表表单部分输出 **/
	public function actionAbsentHtml()
	{
		$class_id   = $this->get->class_id;
		$subject_id = $this->get->subject_id;
		$staff_id   = $this->get->staff_id;


		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		$class = $this->model->class->getIni();
		$subject = $this->model->subject->getIni();

		// 本次id
		$box_id = $this->model->syllabus->getBox($semester_id, $class_id, 1);

		$set_data = $this->model->syllabus->getSet();
		$apm = $set_data['set_am'] + $set_data['set_pm'];
		// 生成模板
		foreach ($set_data['set_week'] as $key => $value){
			$week_lists[$value] = $this->week_ini[$value];
			for ($i=1; $i<= $apm; $i++){
				$data_lists[$i][$value] = "";
			}
		}


		$syllabus_lists = $this->db->fetchAll("SELECT * FROM `tbl_syllabus` WHERE box_id='{$box_id}' AND class_id='{$class_id}' AND subject_id='{$subject_id}' AND staff_id='{$staff_id}'");
		foreach ($syllabus_lists as $key => $value){
			$data_lists[$value['syllabus_section']][$value['syllabus_week']]['name'] = $subject[$value['subject_id']]." ".$class[$value['class_id']];
			$data_lists[$value['syllabus_section']][$value['syllabus_week']]['id']   = $value['syllabus_section'];
		}


		$this->tpl->assign("week_lists", $week_lists);
		$this->tpl->assign("data_lists", $data_lists);
		$this->tpl->display("syllabus/syllabus.absenthtml.tpl.php");
	}

	/** 首页 **/
	public function actionIndex()
	{

		$this->tpl->display();
	}
	/** 课程表基本设置 **/
	public function actionSet()
	{
		$set_id = 1;

		if ($_POST){

			$set_am     			= $this->post->set_am;
			$set_pm     			= $this->post->set_pm;
			$set_week     			= $this->post->set_week;
			$set_double     	    = $this->post->set_double;
			$set_free     			= $this->post->set_free;

			// 序列化数组
			$set_week = serialize($set_week);
			$set_free = serialize($set_free);
			// 写入数据库
			$info = array(

			"set_am"      			=> $set_am,
			"set_pm"				=> $set_pm,
			"set_week"				=> $set_week,
			"set_double"			=> $set_double,
			"set_free"				=> $set_free,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",
			);
			$this->db->do_update("tbl_syllabus_set", $info, "set_id='{$set_id}'");

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=syllabus.set");
		}

		$set_data = $this->model->syllabus->getSet();
		foreach($this->week_ini as $key => $value){
			$week_lists[$key]['name'] = $value;
			if(in_array($key, (array)$set_data['set_week'])){
				$week_lists[$key]['is_check'] = 1;
			}
		}

		foreach($this->free_ini as $key => $value){
			$free_lists[$key]['name'] = $value;
			if(in_array($key, (array)$set_data['set_free'])){
				$free_lists[$key]['is_check'] = 1;
			}
		}

		$this->tpl->assign("set_data", $set_data);
		$this->tpl->assign("week_lists", $week_lists);
		$this->tpl->assign("free_lists", $free_lists);
		$this->tpl->display('syllabus/syllabus.set.tpl.php');
	}


	/** 课时查询 - 周总 分页**/
	public function actionStaffList ()
	{
		$staff_name = $this->get->staff_name;
		$where = "";
		if (!empty($staff_name)){
			$where .= " AND b.staff_name LIKE '%{$staff_name}%' ";
			$this->tpl->assign("staff_name", $staff_name);
		}

		// 本学期课程表所有任课老师
		if ($where != "") {
			$staff_slists = $this->db->fetchAll("SELECT a.staff_id,a.week_hour,b.staff_name,b.staff_number FROM `tbl_staff_subject` AS a,`tbl_staff` AS b WHERE a.staff_id=b.staff_id {$where} GROUP BY staff_id ");
		}
		
		$class = $this->model->class->getIni();
		$subject = $this->model->subject->getIni();

        if(is_array($staff_slists)){
            foreach ($staff_slists as $key => $value){
                // 每个staff的排课记录 subject.class +=1;
                $syllabus_lists = $this->db->fetchAll("SELECT * FROM `tbl_syllabus` WHERE staff_id='{$value['staff_id']}'");

                $a = 0;
                $syllabus = array();
                foreach ($syllabus_lists as $k => $v){
                    $sy[$subject[$v['subject_id']]]++;

                    $syllabus[$subject[$v['subject_id']]]['class'][$class[$v['class_id']]] += 1;
                    $staff_slists[$key]['syllabus'] = $syllabus;
                    $a++;
                }
                foreach ($syllabus as $k => $v){
                    $staff_slists[$key]['syllabus'][$k]['hour'] = array_sum($v['class']);
                }

                $staff_slists[$key]['hour_all'] = $a;
            }
        }


		$this->tpl->assign("staff_slists", $staff_slists);
		$this->tpl->display('syllabus/m.syllabus.stafflist.tpl.php');
	}

	/** 班级课程表列表 **/
	public function actionClassList()
	{
		$grade_lists = $this->model->grade->getall();
		$class_id   = (int)$this->get->class_id;
		$class_data = $this->model->class->getGInfo($class_id);

		// 课程表配置
		$set_data = $this->model->syllabus->getSet();

		if (!empty($class_data)){

			// 学期列表倒序
			$semester_lists = $this->db->fetchAll("SELECT * FROM `tbl_semester` ORDER BY semester_id DESC");

			// 组成列表
			$syllabus_lists = array();
			foreach ($semester_lists as $key => $value){
				$syllabus_lists[$value['semester_id']][1] = array(
				"semester_name" => $value['semester_xname'].$this->semester_name[$value['semester_name']],
				"double_name" => "单周",
				);
				// 单双周
				if ($set_data['set_double'] == 2){
					$syllabus_lists[$value['semester_id']][2] = array(
					"semester_name" => $value['semester_xname'].$this->semester_name[$value['semester_name']],
					"double_name" => "双周",
					);
				}

			}
		}

		// 年级列表 班级列表
		$class_lists = $this->model->class->getTree();

		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->assign("class_data", $class_data);
		$this->tpl->assign("class_id", $class_id);
		$this->tpl->assign("grade_lists", $grade_lists);
		
		$this->tpl->assign("syllabus_lists", $syllabus_lists);
		$this->tpl->display("syllabus/m.syllabus.classlist.tpl.php");

	}

	/** 年级课程列表 **/
	public function actionGradeList()
	{
		$grade_id = (int)$this->get->grade_id;

		$grade_data = $this->model->grade->getInfo($grade_id);

		// 年级列表
		$grade_lists = $this->model->grade->getAll();

		// 课程表配置
		$set_data = $this->model->syllabus->getSet();
		$set_data['set_apm'] = $set_data['set_am'] + $set_data['set_pm'];
		for ($i=1 ; $i <= $set_data['set_apm']; $i++){
			$section_lists[] = $i;
		}
		foreach ($set_data['set_week'] as $key => $value){
			$week_lists[$value] = $this->week_ini[$value];
		}
		// 最新学期
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];


		// 课程
		$subject = $this->model->subject->getIni();
		$special = $this->model->special->getIni();

		// 年级下的班级列表 联查
		$syllabus_lists = $this->db->fetchAll("SELECT a.*,b.class_name FROM `tbl_syllabus` AS a,`tbl_class` AS b WHERE a.class_id=b.class_id AND b.grade_id='{$grade_id}' AND a.semester_id='{$semester_id}' ");

		// 补全数组
		foreach ($syllabus_lists as $key => $value){
			foreach ($week_lists as $k => $v){
				for ($i=1 ; $i <= $set_data['set_apm']; $i++){
					$syllabus[$grade_data['grade_name'].$value['class_name']."_".$this->syllabus_week[$value['syllabus_double']]][$k][$i] = "";
				}
			}
		}

		foreach ($syllabus_lists as $key => $value){

			if ($value['subject_id'] >= 0){
				$syllabus[$grade_data['grade_name'].$value['class_name']."_".$this->syllabus_week[$value['syllabus_double']]][$value['syllabus_week']][$value['syllabus_section']] = $subject[$value['subject_id']];
			}else{
				$syllabus[$grade_data['grade_name'].$value['class_name']."_".$this->syllabus_week[$value['syllabus_double']]][$value['syllabus_week']][$value['syllabus_section']] = $special[abs($value['subject_id'])];
			}
		}

		$this->tpl->assign("grade_id", $grade_id);
		$this->tpl->assign("syllabus", $syllabus);
		$this->tpl->assign("grade_data", $grade_data);
		$this->tpl->assign("set_data", $set_data);
		$this->tpl->assign("week_lists", $week_lists);
		$this->tpl->assign("section_lists", $section_lists);
		$this->tpl->assign("grade_lists", $grade_lists);
		$this->tpl->display("syllabus/syllabus.gradelist.tpl.php");
	}

	/** 教师个人课程表 **/
	public function actionStaffInfo()
	{
		$staff_id = (int)$this->get->staff_id;
		$syllabus_double = (int)$this->get->syllabus_double;
		if (empty($syllabus_double)){
			$syllabus_double = 1;
		}

		// 教师数据
		$staff_data = $this->model->staff->getInfo($staff_id);

		// 最新学期
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		// 课程
		$subject_lists = $this->db->fetchAll("SELECT subject_id,subject_name FROM `tbl_subject`");
		foreach ($subject_lists as $key => $value){
			$subject[$value['subject_id']] = $value['subject_name'];
		}

		// 配置信息
		$set_data = $this->model->syllabus->getSet();

		// 是否启用单双周

		// staff的当前学期syllabus所有
		$syllabus_lists = $this->db->fetchAll("SELECT * FROM `tbl_syllabus` WHERE staff_id='{$staff_id}' AND semester_id='{$semester_id}' AND syllabus_double='{$syllabus_double}'");


		foreach ($syllabus_lists as $key => $value){
			$classa["{$semester_id}_{$value['class_id']}_{$value['syllabus_double']}"] = $this->model->syllabus->getBox($semester_id, $value['class_id'], $value['syllabus_double']);
		}

		foreach ($syllabus_lists as $key => $value){
			if(!empty($classa["{$semester_id}_{$value['class_id']}_{$value['syllabus_double']}"]) && $value['box_id'] == $classa["{$semester_id}_{$value['class_id']}_{$value['syllabus_double']}"]){
				// 每个老师带的班不会太多 so在循环中加入
				$class = $this->model->class->getGInfo($class_id);

				$syllabus[$value['syllabus_section']][$value['syllabus_week']]['class_name']   = $class['grade_name'].$class['class_name'];
				$syllabus[$value['syllabus_section']][$value['syllabus_week']]['subject_name'] = $subject[$value['subject_id']];
			}

		}
		foreach ($set_data['set_week'] as $key => $value){
			$week_lists[$value] = $this->week_ini[$value];

			// 上午节次
			for ($i = 1; $i <= $set_data['set_am']; $i++){
				$am_lists[$i] = 1;
				$out_am[$i][$value] = $syllabus[$i][$value];
			}
			// 下午节次
			$pm_start = $i;
			$pm_end  = $pm_start + $set_data['set_pm'];
			for ($i = $pm_start; $i < $pm_end; $i++){
				$pm_lists[$i] = 1;
				$out_pm[$i][$value] = $syllabus[$i][$value];
			};
		}


		$this->tpl->assign("staff_lists", $this->model->staff->getAll());
		$this->tpl->assign("syllabus_double", $syllabus_double);
		$this->tpl->assign("set_data", $set_data);
		$this->tpl->assign("staff_data", $staff_data);
		$this->tpl->assign("semester_data", $semester_data);
		$this->tpl->assign("out_am", $out_am);
		$this->tpl->assign("out_pm", $out_pm);
		$this->tpl->assign("week_lists", $week_lists);
		$this->tpl->display("syllabus/m.syllabus.staffinfo.tpl.php");
	}

	/** 班级课程表历史 **/
	public function actionInfoList()
	{
		$box_id           = (int)$this->get->box_id;
		$class_id         = (int)$this->get->class_id;
		$semester_id      = (int)$this->get->semester_id;
		$syllabus_double  = (int)$this->get->syllabus_double;

		$semester_data = $this->model->semester->getInfo($semester_id);

		$staff   = $this->model->staff->getIni();
		$subject = $this->model->subject->getIni();

		$syllabus_lists = $this->db->fetchAll("SELECT * FROM `tbl_syllabus` WHERE box_id='{$box_id}' AND class_id='{$class_id}' AND semester_id='{$semester_id}' AND syllabus_double='{$syllabus_double}'");
		foreach ($syllabus_lists as $key => $value){
			$syllabus[$value['syllabus_section']][$value['syllabus_week']]['staff_name']   = $staff[$value['staff_id']];
			$syllabus[$value['syllabus_section']][$value['syllabus_week']]['subject_name'] = $subject[$value['subject_id']];
		}

		// 一周每天
		$set_data = $this->model->syllabus->getSet();
		foreach ($set_data['set_week'] as $key => $value){
			$week_lists[$value] = $this->week_ini[$value];

			// 上午节次
			for ($i = 1; $i <= $set_data['set_am']; $i++){
				$am_lists[$i] = 1;
				$out_am[$i][$value] = $syllabus[$i][$value];
			}
			// 下午节次
			$pm_start = $i;
			$pm_end  = $pm_start + $set_data['set_pm'];
			for ($i = $pm_start; $i <= $pm_end; $i++){
				$pm_lists[$i] = 1;
				$out_pm[$i][$value] = $syllabus[$i][$value];
			};


		}

		$class_data = $this->model->class->getGInfo($class_id);

		// 生效日期列表
		$start_lists = $this->model->syllabus->getStartList();

		// 学期列表
		$semester_lists = $this->model->semester->getAll();

		$this->tpl->assign("class_id", $class_id);
		$this->tpl->assign("semester_id", $semester_id);
		$this->tpl->assign("syllabus_double", $syllabus_double);
		$this->tpl->assign("box_id", $box_id);
		$this->tpl->assign("start_lists", $start_lists);
		$this->tpl->assign("semester_lists", $semester_lists);
		$this->tpl->assign("set_data", $set_data);
		$this->tpl->assign("class_data", $class_data);
		$this->tpl->assign("semester_data", $semester_data);
		$this->tpl->assign("out_am", $out_am);
		$this->tpl->assign("out_pm", $out_pm);
		$this->tpl->assign("week_lists", $week_lists);
		$this->tpl->display("syllabus/syllabus.infolist.tpl.php");

	}



	/** 课程表详情 **/
	public function actionInfo()
	{

		$class_id         = (int)$this->get->class_id;
		$semester_id      = (int)$this->get->semester_id;
		$syllabus_double  = (int)$this->get->syllabus_double;

		$semester_data = $this->model->semester->getInfo($semester_id);

		// 本次id
		$box_id = $this->model->syllabus->getBox($semester_id, $class_id, $syllabus_double);

		$staff   = $this->model->staff->getIni();
		$subject = $this->model->subject->getIni();
		$special = $this->model->special->getIni();

		$syllabus_lists = $this->db->fetchAll("SELECT * FROM `tbl_syllabus` WHERE box_id='{$box_id}' AND class_id='{$class_id}' AND semester_id='{$semester_id}' AND syllabus_double='{$syllabus_double}'");
		foreach ($syllabus_lists as $key => $value){
			// 是否有代课
			$absent = $this->model->absent->getInfo($semester_id, $class_id, $value['syllabus_week'], $value['syllabus_section'], $value['staff_id']);

			if (!empty($absent)){
				$value['staff_id'] = $absent['to_staff'];
			}


			$syllabus[$value['syllabus_section']][$value['syllabus_week']]['staff_name']   = $staff[$value['staff_id']];
			if ($value['subject_id'] > 0 ){
				$syllabus[$value['syllabus_section']][$value['syllabus_week']]['subject_name'] = $subject[$value['subject_id']];
			}else{
				$syllabus[$value['syllabus_section']][$value['syllabus_week']]['subject_name'] = $special[abs($value['subject_id'])];
			}
		}
		$start_time  = $value['start_time'];
		if (strtotime($start_time) > time() || $start_time=="0000-00-00" || empty($start_time)){
			$this->tpl->assign("is_update", "1");
		}

		// 一周每天
		$set_data = $this->model->syllabus->getSet();
		foreach ($set_data['set_week'] as $key => $value){
			$week_lists[$value] = $this->week_ini[$value];

			// 上午节次
			for ($i = 1; $i <= $set_data['set_am']; $i++){
				$am_lists[$i] = 1;
				$out_am[$i][$value] = $syllabus[$i][$value];
			}
			// 下午节次
			$pm_start = $i;
			$pm_end  = $pm_start + $set_data['set_pm'];
			for ($i = $pm_start; $i < $pm_end; $i++){
				$pm_lists[$i] = 1;
				$out_pm[$i][$value] = $syllabus[$i][$value];
			};


		}

		$class_data = $this->model->class->getGInfo($class_id);

		$this->tpl->assign("start_time", $start_time);
		$this->tpl->assign("class_id", $class_id);
		$this->tpl->assign("semester_id", $semester_id);
		$this->tpl->assign("syllabus_double", $syllabus_double);
		$this->tpl->assign("set_data", $set_data);
		$this->tpl->assign("class_data", $class_data);
		$this->tpl->assign("semester_data", $semester_data);
		$this->tpl->assign("out_am", $out_am);
		$this->tpl->assign("out_pm", $out_pm);
		$this->tpl->assign("week_lists", $week_lists);
		$this->tpl->display("syllabus/m.syllabus.info.tpl.php");
	}

	/** 课程表设置 **/
	public function actionUpdate()
	{

		$class_id        = (int)$this->get->class_id;
		$semester_id     = (int)$this->get->semester_id;
		$syllabus_double = (int)$this->get->syllabus_double;

		// 本次id
		$box_id = $this->model->syllabus->getBox($semester_id, $class_id, $syllabus_double);

		// 本班科任指派老师列表
		$staff_slists = $this->db->fetchAll("SELECT id,staff_id,subject_id,week_hour,hour_count FROM `tbl_staff_subject` WHERE class_id='{$class_id}'");


		foreach ($staff_slists as $key => $value){
			$staff_data   = $this->model->staff->getInfo($value['staff_id']);
			$subject_data = $this->model->subject->getInfo($value['subject_id']);
			$staff_slists[$key]['staff_name']   = $staff_data['staff_name'];
			$staff_slists[$key]['subject_name'] = $subject_data['subject_name'];
			// 已用总课时
			$row = $this->db->fetchRow("SELECT SUM(hour_count) AS hour_count FROM `tbl_staff_subject` WHERE staff_id='{$value['staff_id']}'");
			$staff_slists[$key]['week_hour'] = $value['week_hour'] - $row['hour_count'];
		}

		// 一周每天
		$set_data = $this->model->syllabus->getSet();
		foreach ($set_data['set_week'] as $key => $value){
			$week_lists[$value]['name'] = $this->week_ini[$value];
		}
		// 上午节次
		for ($i = 1; $i <= $set_data['set_am']; $i++){
			$am_lists[$i] = array();
		}
		// 下午节次
		$pm_start = $i;
		$pm_end  = $pm_start + $set_data['set_pm'];
		for ($i = $pm_start; $i < $pm_end; $i++){
			$pm_lists[$i] = array();
		}

		if ($_POST){
			//print_r($_POST);exit;


			$box_id = $box_id+1;
			foreach ($week_lists as $key => $value){
				foreach ($am_lists as $k => $v){

					// 派课id
					$id = $_POST["sb_{$key}_{$k}"];
					// 添加记录
					$info = array(
					"syllabus_week"    => $key,
					"syllabus_section" => $k,
					);
					$this->model->syllabus->add($id, $box_id, $class_id, $semester_id, $syllabus_double, $info);
				}
				foreach ($pm_lists as $k => $v){
					// 派课id
					$id = $_POST["sb_{$key}_{$k}"];
					// 添加记录
					// 添加记录
					$info = array(
					"syllabus_week"    => $key,
					"syllabus_section" => $k,
					);
					$this->model->syllabus->add($id, $box_id, $class_id, $semester_id, $syllabus_double, $info);

				}

			}

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=syllabus.info&class_id={$class_id}&semester_id={$semester_id}&syllabus_double={$syllabus_double}");

		}

		$semester_data = $this->model->semester->getInfo($semester_id);

		$class_data = $this->model->class->getGInfo($class_id);
		$start_time = date("Y-m-d");


		foreach ($am_lists as $key => $value){
			foreach ($week_lists as $k => $v){
				$am_lists[$key][$k]['satff'] = "";
				$am_lists[$key][$k]['subject'] = "";
			}
		}
		foreach ($pm_lists as $key => $value){
			foreach ($week_lists as $k => $v){
				$pm_lists[$key][$k]['satff'] = "";
				$pm_lists[$key][$k]['subject'] = "";
			}
		}

		// 选中
		$syllabus_lists = $this->db->fetchAll("SELECT staff_id,subject_id,syllabus_week,syllabus_section,start_time FROM `tbl_syllabus` WHERE box_id='{$box_id}' AND class_id='{$class_id}' AND semester_id='{$semester_id}' AND syllabus_double='{$syllabus_double}'");
		if (!empty($syllabus_lists)){
			$start_time = $syllabus_lists[0]['start_time'];
		}

		foreach ($syllabus_lists as $key => $value){
			if (isset($am_lists[$value['syllabus_section']][$value['syllabus_week']])){
				$am_lists[$value['syllabus_section']][$value['syllabus_week']]['staff']   = $value['staff_id'];
				$am_lists[$value['syllabus_section']][$value['syllabus_week']]['subject'] = $value['subject_id'];
			}
			if (isset($pm_lists[$value['syllabus_section']][$value['syllabus_week']])){
				$pm_lists[$value['syllabus_section']][$value['syllabus_week']]['staff']   = $value['staff_id'];
				$pm_lists[$value['syllabus_section']][$value['syllabus_week']]['subject'] = $value['subject_id'];
			}
		}

		// 列出特殊课堂
		$special_lists = $this->model->special->getList($class_id);

		$this->tpl->assign("start_time", $start_time);
		$this->tpl->assign("staff_slists", $staff_slists);
		$this->tpl->assign("class_data", $class_data);
		$this->tpl->assign("semester_data", $semester_data);
		$this->tpl->assign("set_data", $set_data);
		$this->tpl->assign("am_lists", $am_lists);
		$this->tpl->assign("pm_lists", $pm_lists);
		$this->tpl->assign("week_lists", $week_lists);
		$this->tpl->assign("special_lists", $special_lists);
		$this->tpl->display("syllabus/syllabus.update.tpl.php");
	}

	/** 年级修改汇总 **/
	public function actionGradeUpdate()
	{
		$grade_id = (int)$this->get->grade_id;
		$grade_data = $this->model->grade->getInfo($grade_id);

		// 年级列表
		$grade_lists = $this->model->grade->getAll();

		// 课程表配置
		$set_data = $this->model->syllabus->getSet();
		$set_data['set_apm'] = $set_data['set_am'] + $set_data['set_pm'];
		for ($i=1 ; $i <= $set_data['set_apm']; $i++){
			$section_lists[] = $i;
		}
		foreach ($set_data['set_week'] as $key => $value){
			$week_lists[$value] = $this->week_ini[$value];
		}
		// 最新学期
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];


		// 课程
		$subject_lists = $this->db->fetchAll("SELECT subject_id,subject_name FROM `tbl_subject`");
		foreach ($subject_lists as $key => $value){
			$subject[$value['subject_id']] = $value['subject_name'];
		}

		// 读出修改
		$lists = $this->db->fetchAll("SELECT a.*,b.grade_id FROM `tbl_syllabus` AS a,`tbl_class` AS b WHERE a.class_id=b.class_id AND b.grade_id='{$grade_id}'");
		foreach ($lists as $key => $value){
			$fix["{$value['class_id']}_{$value['syllabus_week']}_{$value['syllabus_section']}"]["{$value['subject_id']}_{$value['staff_id']}"]++;
		}


		// 年级下的班级列表 联查
		$syllabus_lists = $this->db->fetchAll("SELECT a.*,b.class_name FROM `tbl_syllabus` AS a,`tbl_class` AS b WHERE a.class_id=b.class_id AND b.grade_id='{$grade_id}' AND a.semester_id='{$semester_id}' ");

		// 补全数组
		foreach ($syllabus_lists as $key => $value){
			foreach ($week_lists as $k => $v){
				for ($i=1 ; $i <= $set_data['set_apm']; $i++){
					$syllabus[$grade_data['grade_name'].$value['class_name']."_".$this->syllabus_week[$value['syllabus_double']]][$k][$i] = "";
				}
			}
			$classa["{$semester_id}_{$value['class_id']}_{$value['syllabus_double']}"] = $this->model->syllabus->getBox($semester_id, $value['class_id'], $value['syllabus_double']);
		}

		foreach ($syllabus_lists as $key => $value){
			if (count($fix["{$value['class_id']}_{$value['syllabus_week']}_{$value['syllabus_section']}"]) > 1	){
				$syllabus[$grade_data['grade_name'].$value['class_name']."_".$this->syllabus_week[$value['syllabus_double']]][$value['syllabus_week']][$value['syllabus_section']] = "<font color='red'>".$subject[$value['subject_id']]."</font>";
			}else{
				if(!empty($classa["{$semester_id}_{$value['class_id']}_{$value['syllabus_double']}"]) && $value['box_id'] == $classa["{$semester_id}_{$value['class_id']}_{$value['syllabus_double']}"]){
					$syllabus[$grade_data['grade_name'].$value['class_name']."_".$this->syllabus_week[$value['syllabus_double']]][$value['syllabus_week']][$value['syllabus_section']] = $subject[$value['subject_id']];
				}
			}
		}


		$this->tpl->assign("syllabus", $syllabus);
		$this->tpl->assign("grade_data", $grade_data);
		$this->tpl->assign("set_data", $set_data);
		$this->tpl->assign("week_lists", $week_lists);
		$this->tpl->assign("section_lists", $section_lists);
		$this->tpl->assign("grade_lists", $grade_lists);
		$this->tpl->assign("grade_id", $grade_id);

		$this->tpl->display("syllabus/syllabus.gradeupdate.tpl.php");
	}

	/** 发布课程表 **/
	public function actionPub()
	{
		$grade_id   = $this->post->grade_id;
		$start_time = $this->post->start_time;

		// 最新学期
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		// 年级下所有class的最新一次课程表
		$class_lists = $this->model->class->getListByGrade($grade_id);

		foreach ($class_lists as $key => $value){
			$class_id = $value['class_id'];
			for ($i = 1; $i<=2; $i++){
				$box_id = $this->model->syllabus->getBox($semester_id, $class_id, $i);

				// 更新发布时间
				$info = array(
				"start_time"  => $start_time,
				"update_time" => date("Y-m-d H:i:s"),
				);
				$this->db->do_update("tbl_syllabus", $info, "semester_id='{$semester_id}' AND class_id='{$class_id}' AND box_id='{$box_id}' AND syllabus_double='{$i}'");

			}

		}


		// 跳转
		Helper_Client::location("发布成功", "index.php?a=syllabus.gradelist&grade_id={$grade_id}");



	}




	/** 教师课时学月汇总 **/
	public function actionMonth()
	{

		// 当前学期
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		// 学期内总周数
		$semester_week = $this->model->semester->getWeekCount($semester_id);

		// 当前学月
		$semester_month = $this->get->semester_month;
		if (empty($semester_month)){
			$row = $this->model->semester->getWeekByTime(date("Y-m-d"));
			$semester_month = (int)$row['semester_month'];
		}


		// 学月内总周数
		$month_week = $this->model->semester->getWeekCount($semester_id, $semester_month);


		// 当前box_id
		$box_id = $this->model->syllabus->getBox($semester_id);


		// 学月列表
		$month_lists = $this->model->semester->getMonthAll($semester_id);

		// 全校教师
		$staff_lists = $this->model->staff->getAll();
		foreach ($staff_lists as $key => $value){
			$data_lists[$value['staff_id']]['staff_name']     = $value['staff_name'];
			$data_lists[$value['staff_id']]['staff_number']   = $value['staff_number'];
			$data_lists[$value['staff_id']]['week_count']     = 0;
			$data_lists[$value['staff_id']]['month_count']    = 0;
			$data_lists[$value['staff_id']]['semester_count'] = 0;
		}

		// 当前时间段所有任课教师
		$syllabus_lists = $this->db->fetchAll("SELECT staff_id,COUNT(syllabus_id) AS count FROM `tbl_syllabus` WHERE box_id='{$box_id}' AND semester_id='{$semester_id}' GROUP BY staff_id");
		foreach ($syllabus_lists as $key => $value){
			$data_lists[$value['staff_id']]['week_count']     = $value['count'];
			$data_lists[$value['staff_id']]['month_count']    = $value['count'] * $month_week;
			$data_lists[$value['staff_id']]['semester_count'] = $value['count'] * $semester_week;
		}

		$this->tpl->assign("month_lists", $month_lists);
		$this->tpl->assign("data_lists", $data_lists);
		$this->tpl->assign("semester_data", $semester_data);
		$this->tpl->assign("semester_month", $semester_month);
		$this->tpl->display("syllabus/syllabus.month.tpl.php");
	}

	/** 教师学期课时汇总 **/
	public function actionSemester()
	{
		$semester_id   = $this->get->semester_id;
		$semester_data = $this->model->semester->getInfo($semester_id);
		if (empty($semester_data)){
			// 当前学期
			$semester_data = $this->model->semester->getNew();
			$semester_id   = $semester_data['semester_id'];

		}


		// 当前box_id
		$box_id = $this->model->syllabus->getBox($semester_id);

		// 学月列表
		$month_lists = $this->model->semester->getMonthAll($semester_id);
//var_dump($month_lists);
		// 学期内总周数
		$semester_week = $this->model->semester->getWeekCount($semester_id);

		// 全校教师
		$staff_lists = $this->model->staff->getAll();
		foreach ($staff_lists as $key => $value){
			$data_lists[$value['staff_id']]['staff_name']   = $value['staff_name'];
			$data_lists[$value['staff_id']]['staff_number'] = $value['staff_number'];
			// 各学月
			foreach ($month_lists as $k => $v){
				$data_lists[$value['staff_id']]['month'][$k] = 0;
			}
			$data_lists[$value['staff_id']]['semester_count'] = 0;
		}

		// 当前时间段所有任课教师
		$syllabus_lists = $this->db->fetchAll("SELECT staff_id,COUNT(syllabus_id) AS count FROM `tbl_syllabus` WHERE box_id='{$box_id}' AND semester_id='{$semester_id}' GROUP BY staff_id");

		foreach ($syllabus_lists as $key => $value){
			$data_lists[$value['staff_id']]['week_count']     = $value['count'];

			// 各学月
			foreach ($month_lists as $k => $v){
				$data_lists[$value['staff_id']]['month'][$k]    = $value['count'] * $v;
			}

			$data_lists[$value['staff_id']]['semester_count'] = $value['count'] * $semester_week;
		}

		$semester_lists = $this->model->semester->getAll();

		$this->tpl->assign("month_lists", $month_lists);
		$this->tpl->assign("semester_lists", $semester_lists);
		$this->tpl->assign("semester_id", $semester_id);
		$this->tpl->assign("data_lists", $data_lists);
		$this->tpl->assign("semester_data", $semester_data);
		$this->tpl->display("syllabus/syllabus.semester.tpl.php");
	}


	/** 班级课程表 **/
	public function actionClassSyllabusList ()
	{

		// 分页
		YOU::loadLib('page', "", '0');

		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_student`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$class_lists = $this->db->fetchAll("SELECT * FROM `tbl_class` ORDER BY class_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

		foreach($class_lists as $key=>$value){

			// 年级名称
			$grade_data = $this->db->fetchRow("SELECT `grade_name` FROM `tbl_grade` WHERE grade_id={$value['grade_id']}");
			$class_lists[$key]['grade_name'] = $grade_data['grade_name'];

			// 学期名称
			$semester_data = $this->db->fetchRow("SELECT `semester_name` FROM `tbl_semester` WHERE semester_id={$value['semester_id']}");
			$class_lists[$key]['semester_name'] = $this->semester_name[$semester_data['semester_name']];

			// 单双周
			$syllabus_data = $this->db->fetchRow("SELECT `syllabus_week` FROM `tbl_class_syllabus`WHERE syllabus_id={$value['syllabus_id']}");
			$class_lists[$key]['syllabus_week'] = $this->syllabus_week[$syllabus_data['syllabus_week']];
		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->display('syllabus/classsyllabus.list.tpl.php');

	}
	/** 班级修改课程表 **/
	public function actionClassSyllabusUpdate()
	{

		$class_id		= (int)$this->get->class_id;
		// 科目
		$subject_list	= $this->db->fetchAll("SELECT `subject_name`,`subject_id` FROM `tbl_subject`");
		// 老师
		$staff_list		= $this->db->fetchAll("SELECT `staff_name`,`staff_id` FROM `tbl_staff`");
		// 课程表基本设置
		$set_data		= $this->db->fetchRow("SELECT * FROM `tbl_syllabus_set`");
		$set_data['set_week']	= unserialize($set_data['set_week']);
		$set_data['set_am']		= unserialize($set_data['set_am']);
		$set_data['set_pm']		= unserialize($set_data['set_pm']);


		if (empty($class_id)){
			Helper_Client::alert("班级不存在");
		}

		if ($_POST){

			$subject_am     	    = $this->post->subject_am;
			$staff_am     			= $this->post->staff_am;
			$subject_pm     	    = $this->post->subject_pm;
			$staff_pm     			= $this->post->staff_pm;
			foreach($subject_am as $key=>$value){

				//$subject_list	= $this->db->fetchAll("SELECT `subject_name`,`subject_id` FROM `tbl_subject` WHERE subject_id = {$value['$key']}");

			}

			// 写入数据库
			$info = array(

			"student_name"      	=> $student_name,
			"student_sex"			=> $student_sex,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",
			);
			$this->db->do_update("tbl_student", $info, "student_id='{$student_id}'");

			// 跳转
			//Helper_Client::location("修改成功", "index.php?a=student.student	list&student_id={$student_id}");
		}

		$this->tpl->assign("subject_list", $subject_list);
		$this->tpl->assign("staff_list", $staff_list);
		$this->tpl->assign("set_data", $set_data);
		$this->tpl->display('syllabus/classsyllabus.update.tpl.php');
	}


	/** 学科课时设置列表 **/
	public function actionHourList()
	{
		$class_id   = $this->get->class_id;
		$subject_id = $this->get->subject_id;

		$where = " WHERE 1=1 ";

		if (!empty($class_id)){
			$where .= " AND class_id = '{$class_id}' ";
			$this->tpl->assign("class_id", $class_id);
		}
		if (!empty($subject_id)){
			$where .= " AND subject_id = '{$subject_id}' ";
			$this->tpl->assign("subject_id", $subject_id);
		}

		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_subject_class`  {$where} ");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$sc_lists = $this->db->fetchAll("SELECT * FROM `tbl_subject_class` {$where}  ORDER BY sc_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
		foreach ($sc_lists as $key => $value){
			$row = $this->model->class->getGinfo($value['class_id']);
			$sc_lists[$key]['grade_name']   = $row['grade_name'];
			$sc_lists[$key]['class_name']   = $row['class_name'];
			$row = $this->model->subject->getInfo($value['subject_id']);
			$sc_lists[$key]['subject_name'] = $row['subject_name'];
		}

		// 班级列表
		$class_lists = $this->model->class->getAll();
		// 学科列表
		$subject_lists = $this->model->subject->getAll();

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("count", $count);
		$this->tpl->assign("sc_lists", $sc_lists);
		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->assign("subject_lists", $subject_lists);


		$this->tpl->display('syllabus/syllabus.hourlist.tpl.php');
	}

	/** 学科课时设置添加 **/
	public function actionHourAdd()
	{

		if ($_POST){
			$subject_id = $this->post->subject_id;
			$week_hour  = $this->post->week_hour;
			$pid        = $this->post->pid;

			foreach ((array)$pid as $key => $value){
				if (substr($value, 0, 1) == "c"){
					$class_id = intval(substr($value, 1));

					// 添加到数据库
					$info = array(
					"subject_id"  => $subject_id,
					"class_id"    => $class_id,
					"week_hour"   => $week_hour,
					"status"      => 1,
					"create_time" => date("Y-m-d H:i:s"),
					"update_time" => "0000-00-00 00:00:00"
					);
					$this->db->do_insert("tbl_subject_class", $info);
				}
			}

			// 跳转
			Helper_Client::location("操作成功", "index.php?a=syllabus.hourlist");
		}


		// 学科列表
		$subject_lists = $this->model->subject->getAll();

		// 年级班级树
		$class_lists = $this->model->class->getTree();

		$this->tpl->assign('class_lists', $class_lists);
		$this->tpl->assign('subject_lists', $subject_lists);
		$this->tpl->display('syllabus/syllabus.houradd.tpl.php');
	}

	/** 课时设置修改 **/
	public function actionHourUpdate()
	{
		$sc_id = (int)$this->get->sc_id;
		$sc_data = $this->db->fetchRow("SELECT * FROM `tbl_subject_class` WHERE sc_id='{$sc_id}'");
		if (empty($sc_data)){
			Helper_Client::alert("数据不存在");
		}
		if ($_POST){
			$week_hour = $this->post->week_hour;

			$info = array(
			"week_hour" => $week_hour,
			"update_time" => date("Y-m-d H:i:s"),
			);
			$this->db->do_update("tbl_subject_class", $info, "sc_id='{$sc_id}'");

			Helper_Client::location("修改成功", "index.php?a=syllabus.hourlist");
		}

		// 年级名称 班级名称
		$row = $this->model->class->getGinfo($sc_data['class_id']);
		$sc_data['grade_name'] = $row['grade_name'];
		$sc_data['class_name'] = $row['class_name'];
		// 学科名称
		$row = $this->model->subject->getInfo($sc_data['subject_id']);
		$sc_data['subject_name'] = $row['subject_name'];


		$this->tpl->assign("sc_data", $sc_data);
		$this->tpl->display("syllabus/syllabus.hourupdate.tpl.php");
	}

	/** 删除课时设置 **/
	public function actionHourDel()
	{
		$sc_id = $this->get->sc_id;

		$sql = "DELETE FROM `tbl_subject_class` WHERE sc_id='{$sc_id}'";
		$this->db->query($sql);

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=syllabus.hourlist");

	}

	/** 导入 **/
	public function actionIn()
	{
		$class_id        = (int)$this->get->class_id;
		$semester_id     = (int)$this->get->semester_id;
		$syllabus_double = (int)$this->get->syllabus_double;

		// 学期
		$semester_data = $this->model->semester->getInfo($semester_id);
		// 班级
		$class_data = $this->model->class->getInfo($class_id);

		// 课程表配置
		$set_data = $this->model->syllabus->getSet();
		// 星期
		$week[0] = "节次";
		foreach ($set_data['set_week'] as $key => $value){
			$week[$value] = $this->week_ini[$value];
		}


		$this->model->excel->excel_ini['syllabus']['col'] = $week;

		// 本次id
		$box_id = $this->model->syllabus->getBox($semester_id, $class_id, $syllabus_double);


		if ($_POST){
			$box_id = $box_id+1;

			// 上传excel
			$data_lists = $this->model->excel->in("syllabus");


			foreach ($data_lists as $key => $value){
				foreach ($value as $k => $v){
					if ($k == "1"){
						$section = $v;
					}else{
						$w = $k - 1;
						if (empty($week[$w])) continue;

						$info = array(
						"syllabus_week"    => $w,
						"syllabus_section" => $section,
						);
						$id = $v;

						$tmp = explode("-", $v);
						$staff_name = trim($tmp[0]);
						$subject_name = trim($tmp[1]);

						$this->model->syllabus->in($staff_name, $subject_name, $box_id, $class_id, $semester_id, $syllabus_double, $info);

					}
				}

			}

			// 跳转 导入成功
			Helper_Client::location("添加成功", "index.php?a=syllabus.info&semester_id={$semester_id}&class_id={$class_id}&syllabus_double={$syllabus_double}");


		}


		$this->tpl->assign("semester_data", $semester_data);
		$this->tpl->assign("class_data", $class_data);
		$this->tpl->assign("class_id", $class_id);
		$this->tpl->assign("semester_id", $semester_id);
		$this->tpl->display("syllabus/syllabus.in.tpl.php");

	}

	/** 导出模板 **/
	public function actionOutTp()
	{

		$semester_id = $this->get->semester_id;
		$class_id    = $this->get->class_id;

		$semester_data = $this->model->semester->getInfo($semester_id);
		$class_data = $this->model->class->getInfo($class_id);

		// 模板标题
		$this->model->excel->excel_ini['syllabus']['title'] = "{$semester_data['semester_xname']}{$semester_data['semester_name']}-{$class_data['class_name']}班级课程表";

		$set_data = $this->model->syllabus->getSet();
		// 星期
		$week[0] = "节次";
		foreach ($set_data['set_week'] as $key => $value){
			$week[$value] = $this->week_ini[$value];
		}
		$this->model->excel->excel_ini['syllabus']['col'] = $week;

		// 节次
		$apm = $set_data['set_am'] + $set_data['set_pm'];
		$a = $this->model->excel->excel_ini['syllabus']['start'];
		for ($i = 1; $i<= $apm; $i++){
			$data_lists[$a][] = $i;
			$a++;
		}

		$this->model->excel->excel_ini['syllabus']['line'] = $apm;

		// 导出模板 - 6行空数据
		$this->model->excel->outData("syllabus", $data_lists);





	}


	/**  导出数据 **/
	public function actionOutData()
	{
		ini_set("display_errors", "off");
		$class_id         = (int)$this->get->class_id;
		$semester_id      = (int)$this->get->semester_id;
		$syllabus_double  = (int)$this->get->syllabus_double;

		$semester_data = $this->model->semester->getInfo($semester_id);
		$class_data = $this->model->class->getInfo($class_id);

		// 本次id
		$box_id = $this->model->syllabus->getBox($semester_id, $class_id, $syllabus_double);

		$staff   = $this->model->staff->getIni();
		$subject = $this->model->subject->getIni();

		$set_data = $this->model->syllabus->getSet();
		// 星期
		$week[0] = "节次";
		foreach ($set_data['set_week'] as $key => $value){
			$week[$value] = $this->week_ini[$value];
		}

		$syllabus_lists = $this->db->fetchAll("SELECT * FROM `tbl_syllabus` WHERE box_id='{$box_id}' AND class_id='{$class_id}' AND semester_id='{$semester_id}' AND syllabus_double='{$syllabus_double}'");
		// 模板
		foreach ($syllabus_lists as $key => $value){
			$info = array();
			foreach ($week as $k => $v){
				$data_lists[$value['syllabus_section']][$k] = "";
			}
			$data_lists[$value['syllabus_section']][0] = $value['syllabus_section'];
		}


		foreach ($syllabus_lists as $key => $value){
			$data_lists[$value['syllabus_section']][$value['syllabus_week']] = $staff[$value['staff_id']]."-".$subject[$value['subject_id']];
		}

		$start_time  = $value['start_time'];

		// 模板标题
		$this->model->excel->excel_ini['syllabus']['title'] = "{$semester_data['semester_xname']}学年{$semester_data['semester_name']}学期{$class_data['class_name']}课程表({$start_time })";
		$this->model->excel->outData("syllabus", $data_lists);

	}


}




?>